package com.hns.can.service.web;

import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hns.can.common.domain.GridPage;
import com.hns.can.common.vo.CommonParamsVo;
import com.hns.can.common.web.BaseController;
import com.hns.can.service.service.VehicleManageService;
import com.hns.can.service.vo.VehicleTypeCountVo;
import com.hns.frame.mybatis.Pagination;
import com.hns.tool.export.ExportUtil;
import com.hns.tool.export.excel.excelTemplate.ExportExcelTempEntity;
import com.hns.tool.pub.DateTool;

@Controller
@RequestMapping("/vehicleManage")
public class VehicleManageController extends BaseController {

	@Autowired
	private VehicleManageService vehicleManageService;
	
	@RequestMapping("/getVehicleTypeCount")
	@ResponseBody
	public Object getVehicleTypeCount(CommonParamsVo params){
		params.setUserid(this.getCurrentUser().getUser().getId());
		Pagination page = vehicleManageService.getVehicleTypeCount(params);
		GridPage grid = new GridPage(page);
		return this.getReturnResult(grid);
	}
	
	
	@RequestMapping("/getVehicleCountExcel")
	public void getVehicleCountExcel(CommonParamsVo params, HttpServletRequest request, HttpServletResponse response) throws Exception{
		List<VehicleTypeCountVo> list = getVehicleTypeCount4Export(params);
		ExportExcelTempEntity exportEntity = new ExportExcelTempEntity();
        exportEntity.setTempName("vehicleTypeCount.xls");
        exportEntity.setDataSource(list);
        exportEntity.setShowName(URLEncoder.encode("车辆类型统计", "UTF-8")+DateTool.getCurrentSysDate("yyyyMMddHHmmss"));
        ExportUtil.generatorExcelByTemplate(exportEntity, response);
		//commonExportService.exportExcel(ExcelTemplate.VEHICLE_TYPE_COUNT_TEMPLATE.getFlag(), list, "vehicleTypeCount", request, response);
	}
	
	
	@RequestMapping("/getVehicleCountPdf")
	public void getVehicleCountPdf(CommonParamsVo params,
		HttpServletRequest request, HttpServletResponse response) throws Exception{
		String[] tableHeaderName = new String[]{"组织名称","线路名称","车辆数量","传统车小计","柴油车","汽油车","新能源车小计","混合动力车","纯电动车","CNG","LNG"};
		String[] tablePropertyName = new String[]{"organName", "lineName", "carNum", "carTotal1Num", "carType1Num", "carType2Num", 
				"carTotal2Num", "carType3Num", "carType4Num", "carType5Num", "carType6Num"};
		List<VehicleTypeCountVo> list = getVehicleTypeCount4Export(params);
		ExportUtil.exportFiles(Arrays.asList(tableHeaderName), Arrays.asList(tablePropertyName), list, response, 
				"pdf", URLEncoder.encode("车辆类型统计", "UTF-8")+DateTool.getCurrentSysDate("yyyyMMddHHmmss"), "车辆故障明细数据");
	}
	
	
	/**
	 * 查询导出的数据
	 * @param params
	 * @return
	 * @author:zhangyu
	 * @email:zhangyu2222z@sina.cn
	 */
	private List<VehicleTypeCountVo> getVehicleTypeCount4Export(CommonParamsVo params) {
		//SysUserEntity currentUser = (SysUserEntity) request.getSession().getAttribute("SES_CURRENTUSER");
		params.setUserid(this.getCurrentUser().getUser().getId());
		List<VehicleTypeCountVo> list = vehicleManageService.getVehicleTypeCount4Export(params);
		return list;
	}
	
}
